package cn.iocoder.yudao.module.trade.service.order.handler;

import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 交易模式处理器工厂
 */
@Component
public class TransactionModeHandlerFactory {
    
    private final Map<Integer, TransactionModeHandler> handlerMap = new ConcurrentHashMap<>();
    
    public TransactionModeHandlerFactory(List<TransactionModeHandler> handlers) {
        // 初始化处理器映射
        for (TransactionModeHandler handler : handlers) {
            handlerMap.put(handler.getTransactionType(), handler);
        }
    }
    
    /**
     * 获取交易模式处理器
     *
     * @param transactionType 交易类型
     * @return 处理器
     */
    public TransactionModeHandler getHandler(Integer transactionType) {
        TransactionModeHandler handler = handlerMap.get(transactionType);
        if (handler == null) {
            throw new IllegalArgumentException("Unsupported transaction type: " + transactionType);
        }
        return handler;
    }
    
    /**
     * 注册新的交易模式处理器
     *
     * @param handler 处理器
     */
    public void registerHandler(TransactionModeHandler handler) {
        handlerMap.put(handler.getTransactionType(), handler);
    }
} 